﻿/*
 * Copyright (C) 2011, EADS France
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * Author: Laurent WOUTERS
 */

using System;
using System.Collections.Generic;
using System.Text;
using Hime.Redist.Parsers;

namespace xowl.gmi.compiler.transform
{
    class Constant : PatternPart
    {
        private string value;

        public Constant(string value) { this.value = value; }
        public Constant(SyntaxTreeNode ast)
        {
            if (ast.Symbol.Name == "entity")
            {
                value = ((SymbolTokenText)ast.Children[0].Symbol).ValueText;
                if (ast.Children.Count > 1)
                    value += ":" + ((SymbolTokenText)ast.Children[1].Symbol).ValueText;
            }
            else if (ast.Symbol.Name == "STRING")
                value = ((SymbolTokenText)ast.Symbol).ValueText;
            else if (ast.Symbol.Name == "NUMBER")
            {
                value = ((SymbolTokenText)ast.Symbol).ValueText;
                if (value.Contains("."))
                    value = "\"" + value + "\"^^xsd:float";
                else if (value.StartsWith("0x"))
                    value = Int32.Parse(value.Substring(2), System.Globalization.NumberStyles.HexNumber).ToString();
                else if (value == "true" | value == "false")
                    value = "\"" + value + "\"^^xsd:boolean";
            }
        }

        public void Write(System.IO.StreamWriter writer) { writer.Write(value); }
        public void WriteOWL(System.IO.StreamWriter writer) { writer.Write(value); }

        public override string ToString() { return value; }
    }
}
